package com.yunshipei.model;

import android.content.Context;
import android.util.Log;

import com.yunshipei.common.Globals;
import com.yunshipei.inter.NotificationInter;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.utils.AlarmSet;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by cike on 15-11-24.
 */
public class NotifyInfo implements NotificationInter {
    private int _id;            //id
    private String userId = "";     //默认登录者Id
    private SpeechRecognition speechRecognition = new SpeechRecognition();
    private String repetitionTime = "";                     //重复的时间(多个时间时用","隔开)
    private String delayTime = "5";                         //延迟时间间隔(默认间隔5分钟)
    private String forwardTime = "0";                       //提前提醒的时间(默认10分钟)
    private boolean shake = true;                           //是否震动(默认震动)
    private boolean musicStatus = true;                     //音乐状态
    private String music = "";                              //铃声
    private String musicName = "";                          //铃声名称
    private boolean voiceRepore = true;                     //语音播报(默认语音播报)
    private String notifiStatus = "true";                   //是否提醒(默认提醒)
    private boolean repetitionBool[] = new boolean[9];      //重复提醒状态

    public NotifyInfo() {
        this.userId = YspPreferences.getInstance().getSharedPreferences().getString(Globals.UUID, "");
        speechRecognition = new SpeechRecognition();
    }

    public SpeechRecognition getSpeechRecognition() {
        return speechRecognition;
    }

    public void setSpeechRecognition(SpeechRecognition speechRecognition) {
        this.speechRecognition = speechRecognition;
    }

    public String getRepetitionTime() {
        return repetitionTime;
    }

    public void setRepetitionTime(String repetitionTime) {
        this.repetitionTime = repetitionTime;
    }

    public String getDelayTime() {
        return delayTime;
    }

    public void setDelayTime(String delayTime) {
        this.delayTime = delayTime;
    }

    public String getForwardTime() {
        return forwardTime;
    }

    public void setForwardTime(String forwardTime) {
        this.forwardTime = forwardTime;
    }

    public String getMusic() {
        return music;
    }

    public void setMusic(String music) {
        this.music = music;
    }

    public boolean getShake() {
        return shake;
    }

    public void setShake(boolean shake) {
        this.shake = shake;
    }

    public boolean getVoiceRepore() {
        return voiceRepore;
    }

    public void setVoiceRepore(boolean voiceRepore) {
        this.voiceRepore = voiceRepore;
    }

    public String getNotifiStatus() {
        return notifiStatus;
    }

    public void setNotifiStatus(String notifiStatus) {
        this.notifiStatus = notifiStatus;
    }

    public int get_id() {
        return _id;
    }

    public void set_id(int _id) {
        this._id = _id;
    }

    public boolean[] getRepetitionBool() {
        return repetitionBool;
    }

    public void setRepetitionBool(boolean[] repetitionBool) {
        this.repetitionBool = repetitionBool;
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public boolean isShake() {
        return shake;
    }

    public boolean getMusicStatus() {
        return musicStatus;
    }

    public void setMusicStatus(boolean musicStatus) {
        this.musicStatus = musicStatus;
    }

    public boolean isVoiceRepore() {
        return voiceRepore;
    }

    public boolean isMusicStatus() {
        return musicStatus;
    }

    public String getMusicName() {
        return musicName;
    }

    public void setMusicName(String musicName) {
        this.musicName = musicName;
    }

    /**
     * 计算提前提醒时间
     *
     * @return
     */
    public long countForwardTime() {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long forwardTime = this.getSpeechRecognition().getDateTime().getFormatDate()
                - (Integer.parseInt(this.getForwardTime()) * 60 * 1000);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(forwardTime);
        if (calendar.getTime().after(new Date())) {
            return forwardTime;
        } else {
            return this.getSpeechRecognition().getDateTime().getFormatDate();
        }
    }

    /**
     * 新建循环提醒
     */
    public void setAlarm(Context context) {
        String repetitionTime = getRepetitionTime();
        AlarmSet alarmSet = new AlarmSet(context);
        Log.e("repetitionTime", repetitionTime);
        if (repetitionTime.equals("每天")) {
            //设置每天重复提醒
            alarmSet.setAlarm(this, 24 * 60 * 60 * 1000, countForwardTime(), this.get_id());
        } else if (repetitionTime.equals("从不")) {
            //设置一次提醒
            alarmSet.setAlarm(this, countForwardTime(), this.get_id());
        } else if (!repetitionTime.equals("")) {
            for (int i = 0; i < 7; i++) {
                if (getRepetitionBool()[i]) {
                    alarmSet.setAlarm(this, 7 * 24 * 60 * 60 * 1000, countForwardTime(), this.get_id());
                }
            }
        } else {
            Log.e("setAlarm", "无法设置重复提醒");
            alarmSet.setAlarm(this, countForwardTime(), this.get_id());
        }
    }

}
